package com.itboye.system.crypt;

import com.itboye.system.helper.ByConsoleHelper;
import com.itboye.system.helper.ByDateHelper;

import java.io.UnsupportedEncodingException;
import java.security.InvalidParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 博也数据传输加解密方法
 * Created by hebidu on 16/5/9.
 */
public class ByDataCrypt {


    /**
     * Boye签名算法
     * time + type + data + client_secret + notify_id
     * @param params 数据字典
     * @return 签名后的字符串
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static String sign(Map<String,String> params) throws InvalidParameterException,UnsupportedEncodingException, NoSuchAlgorithmException {

        String time = params.get("time");
        if(time == null || time.length() == 0){
            throw new InvalidParameterException("time参数非法!");
        }
        String type = params.get("type");
        if(type == null || type.length() == 0){
            throw new InvalidParameterException("type参数非法");
        }

        String data = params.get("data");
        String alg = params.get("alg");

        if(alg == null || alg.length() == 0){
            throw new InvalidParameterException("alg参数非法!");
        }

        String client_secret = params.get("client_secret");
        if(client_secret == null || client_secret.length() == 0){
            throw new InvalidParameterException("client_secret参数缺少!");
        }

        String notify_id = params.get("notify_id");

        if(notify_id == null || notify_id.length() == 0){
            notify_id = ByDateHelper.getCurrentTimeStamp().toString();
        }

        String text = time + type + data + client_secret + notify_id;

        if(alg.equals("md5")){
            return MD5Crypt.encode(text);
        }

        throw new InvalidParameterException("不支持的算法类型!"+alg);

    }

    /**
     * 对明文进行加密
     * @param text 明文
     * @return 加密后的密文
     */
    public  static  String encode(String text) throws UnsupportedEncodingException {

        String base64Str = new String(Base64.getEncoder().encode(text.getBytes("utf-8")),"utf-8");

        base64Str = new String(Base64.getEncoder().encode(base64Str.getBytes("utf-8")),"utf-8");
        return base64Str;
    }

    /**
     * 对密文进行解密
     * @param text 加密后的密文
     * @return 解密后的明文
     */
    public  static  String decode(String text) throws UnsupportedEncodingException {
        String decodeStr = new String(Base64.getDecoder().decode(text.getBytes()),"utf-8");

        decodeStr = new String(Base64.getDecoder().decode(decodeStr.getBytes()),"utf-8");
        return decodeStr;
    }

    /**
     * 加密数据内容
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encodeData(String data) throws UnsupportedEncodingException {
        String tmp = Base64Crypt.encode(data);
        return  Base64Crypt.encode(tmp);
    }

    /**
     * 解密数据内容
     * @param data
     * @return
     */
    public static HashMap decodeData(String data) throws UnsupportedEncodingException {

        String tmp = Base64Crypt.decode(data);
        ByConsoleHelper.debug("base64解码"+tmp,null);
        tmp = Base64Crypt.decode(tmp);
        ByConsoleHelper.debug("base64解码"+tmp,null);
        HashMap map = JsonCrypt.readValue(tmp,HashMap.class);

        if(map == null){
            map = new HashMap();
        }

        return  map;
    }


}
